package com.itheima.appserver.controller;


import com.itheima.appserver.service.TanhuaService;
import com.itheima.commons.model.vo.NearUserVo;
import com.itheima.commons.model.vo.PageResult;
import com.itheima.commons.model.vo.TodayBestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 交友相关API接口
 */
@RestController
@RequestMapping("tanhua")
public class TanhuaController {

    @Autowired
    private TanhuaService tanhuaService;

    /**
     * 首页今日佳人功能
     *
     * @return
     */
    @GetMapping("todayBest")
    public ResponseEntity todayBest() {
        TodayBestVo todayBest = tanhuaService.todayBest();
        return ResponseEntity.ok(todayBest);
    }

    /**
     * 首页推荐列表功能
     *
     * @param params
     * @return
     */
    @GetMapping("recommendation")
    public ResponseEntity recommendation(@RequestParam Map<String, Object> params) {
        PageResult pageResult = tanhuaService.recommendationList(params);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询个人主页的个人信息
     *
     * @param userId
     * @return
     */
    @GetMapping("{id}/personalInfo")
    public ResponseEntity queryUserInfo(@PathVariable("id") Long userId) {
        TodayBestVo todayBest = tanhuaService.queryUserInfo(userId);
        return ResponseEntity.ok(todayBest);
    }

    /**
     * 查询陌生人问题
     *
     * @param userId
     * @return
     */
    @GetMapping("strangerQuestions")
    public ResponseEntity queryQuestion(@RequestParam("userId") Long userId) {
        String question = tanhuaService.queryQuestion(userId);
        return ResponseEntity.ok(question);
    }

    /**
     * 回复陌生人问题
     *
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity replyQuestion(@RequestBody Map<String, Object> param) {
        // 被申请人的ID
        Long userId = Long.valueOf(param.get("userId").toString());
        // 申请人的回答
        String reply = param.get("reply").toString();
        tanhuaService.replyQuestion(userId, reply);
        return ResponseEntity.ok(null);
    }

    /**
     * 搜附近
     *
     * @param gender
     * @param distance
     * @return
     */
    @GetMapping("search")
    public ResponseEntity queryNearUser(@RequestParam(value = "gender", required = false) String gender,
                                        @RequestParam(value = "distance", defaultValue = "2000") Double distance) {
        List<NearUserVo> list = tanhuaService.queryNearUser(gender, distance);
        return ResponseEntity.ok(list);
    }


    /**
     * 探花
     *
     * @return
     */
    @GetMapping("cards")
    public ResponseEntity queryCardsList() {
        List<TodayBestVo> list = tanhuaService.queryCardsList();
        return ResponseEntity.ok(list);
    }

    /**
     * 喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity likeUser(@PathVariable("id") Long likeUserId) {
        tanhuaService.likeUser(likeUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 不喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity notLikeUser(@PathVariable("id") Long likeUserId) {
        tanhuaService.notLikeUser(likeUserId);
        return ResponseEntity.ok(null);
    }
}
